package org.example.cake.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.apache.ibatis.annotations.Param;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import org.example.cake.entity.Discuss;
import org.example.cake.mapper.DiscussMapper;
import org.example.cake.service.DiscussService;

import java.util.List;

@Service
public class DiscussServiceImpl implements DiscussService{

    @Resource
    private DiscussMapper discussMapper;

    @Override
    public int deleteByPrimaryKey(Integer discussid) {
        return discussMapper.deleteByPrimaryKey(discussid);
    }

    @Override
    public int insert(Discuss record) {
        return discussMapper.insert(record);
    }

    @Override
    public int insertSelective(Discuss record) {
        return discussMapper.insertSelective(record);
    }

    @Override
    public Discuss selectByPrimaryKey(Integer discussid) {
        return discussMapper.selectByPrimaryKey(discussid);
    }

    @Override
    public int updateByPrimaryKeySelective(Discuss record) {
        return discussMapper.updateByPrimaryKeySelective(record);
    }

    @Override
    public int updateByPrimaryKey(Discuss record) {
        return discussMapper.updateByPrimaryKey(record);
    }


    /**
     * 前端方法：根据蛋糕id查询有关评论
     * @param cakeid 蛋糕id
     * @author 李丽君
     * @Date 2025-07-25
     * @return 评论实体
     */
    public List<Discuss> selectByCakeId(Integer cakeid){
        return discussMapper.selectByCakeId(cakeid);
    }

    /**
     * 小程序方法  新增评论
     * @param discuss 评论对象
     * @return  受影响的行数
     */
    public int insertDiscuss(Discuss discuss){
        return discussMapper.insertDiscuss(discuss);
    }


    @Override
    public PageInfo<Discuss> getAllDiscussWithDetails(Integer page, Integer pageSize) {
        // 开启分页
        PageHelper.startPage(page, pageSize);
        // 执行查询
        List<Discuss> discussList = discussMapper.selectAllDiscussWithDetails();
        // 创建分页对象
        return new PageInfo<>(discussList);
    }

    @Override
    public PageInfo<Discuss> getDiscussList(Integer page, Integer pageSize, String keyword) {
        // 开启分页
        PageHelper.startPage(page, pageSize);
        // 执行查询
        List<Discuss> discussList = discussMapper.selectDiscussList(keyword);
        // 创建分页对象
        return new PageInfo<>(discussList);
    }

    @Override
    public boolean deleteDiscuss(Integer discussId) throws Exception {
        // 先删除关联的评论图片
        discussMapper.deleteDiscussImages(discussId);
        // 再删除评论
        int result = discussMapper.deleteDiscussById(discussId);
        return result > 0;
    }

    @Override
    public List<String> getDiscussImages(Integer discussId) {
        return discussMapper.selectDiscussImages(discussId);
    }

    /**
     * 根据订单id查询是否有评论
     * @param orderid 订单id
     * @return 订单集合
     */
    public boolean selectByOrderId(@Param("orderid") Integer orderid){
        return discussMapper.selectByOrderId(orderid);
    }
}
